//
//  XMPlainHeaderFooterSectionView.m
//  XMKit
//
//  Created by chenming on 2017/12/21.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "XMPlainHeaderFooterSectionView.h"

@implementation XMPlainHeaderFooterSectionView

/** 消除Plain中header和footer粘性 */
+ (instancetype)headerViewWithTableView:(UITableView *)tableView {
    static NSString *const cellId = @"XMPlainHeaderFooterSectionView";
    XMPlainHeaderFooterSectionView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellId];
    if(!header) {
        header = [[XMPlainHeaderFooterSectionView alloc] initWithReuseIdentifier:cellId];
        header.contentView.backgroundColor = [UIColor xm_colorWithHexString:@"ecf0f4" alpha:1];
    }
    return header;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

+ (CGFloat)getSectionHeight {
    return 10;
}

/**
 重写frame

 @param frame 指定frame
 */
- (void)setFrame:(CGRect)frame {
    CGRect sectionRect = [self.tableView rectForSection:self.section];
    CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
    [super setFrame:newFrame];
}

// tableview的取消悬停或者是粘滞
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    UITableView *tableview = (UITableView *)scrollView;
    CGFloat sectionHeaderHeight = 20;
    CGFloat sectionFooterHeight = 20;
    CGFloat offsetY = tableview.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight) {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight) {
        tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height) {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
    }
}
/*
    // 适配iOS11系统
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)){
        //避免滚动视图顶部出现20的空白以及push或者pop的时候页面有一个上移或者下移的异常动画的问题
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
*/

@end
